/**
 *
 */
package com.hexacta.hrs.services.staffing.impl;

import java.util.Calendar;

import org.apache.commons.lang.StringUtils;

import com.hexacta.hrs.domain.staffing.assignment.AssignmentType;
import com.hexacta.hrs.domain.staffing.assignment.AvailabilityStatus;

/**
 * Clase que posee la informacion de los empleados y sus asignaciones.
 * 
 * @author ldeveikis
 * 
 */
public class EmployeeAssignmentDTO extends SimpleEmployeeAssignmentDTO {

    private String employeeName;

    private String role;

    private String projectName;

    private Long assignmentID;

    private String tentative;

    private String description;

    private Double FTE;

    // private AssignmentType assignmentType;

    private AvailabilityStatus availabilityStatus; // for not assigned only

    // private boolean internal;

    /**
     * 
     */
    public EmployeeAssignmentDTO() {
        super();
    }

    /**
     * @param from
     * @param to
     * @param employeeID
     * @param employeeName
     * @param role
     * @param porcentage
     * @param availabilityStatus
     * @param projectName
     * @param assignmentID
     * @param tentative
     * @param description
     * @param fTE
     */
    public EmployeeAssignmentDTO(final Calendar from, final Calendar to, final Long employeeID, final String firstName,
            final String middleName, final String lastName, final String role, final Double porcentage,
            final AssignmentType assignmentType, final String projectName, final Long assignmentID,
            final String tentative, final String description, final double fTE, final boolean internal) {
        super(from, to, employeeID, porcentage);

        setEmployeeName(buildName(firstName, middleName, lastName));
        setRole(role);
        setProjectName(projectName);
        setAssignmentID(assignmentID);
        setTentative(tentative);
        setDescription(description);
        setFTE(fTE);
        setAvailabilityStatus(buildAvailabilityStatus(assignmentType, internal));

    }

    public Long getAssignmentID() {
        return assignmentID;
    }

    public void setAssignmentID(final Long assignmentID) {
        this.assignmentID = assignmentID;
    }

    public String getEmployeeName() {
        return employeeName;
    }

    public void setEmployeeName(final String employeeName) {
        this.employeeName = escape(employeeName);
    }

    public String getRole() {
        return role;
    }

    public void setRole(final String role) {
        this.role = role;
    }

    public boolean isNoAsignado() {
        return getAvailabilityStatus() == AvailabilityStatus.NoAsignado;
    }

    public void setProjectName(final String projectName) {
        this.projectName = escape(projectName);
    }

    /**
     * @param name
     * @return
     */
    private String escape(final String name) {
        String escapedName = null;
        if (name != null) {
            escapedName = name.replace("'", "\\'");
        }
        return escapedName;
    }

    public String getProjectName() {
        return projectName;
    }

    public void setTentative(final String tentative) {
        this.tentative = tentative;
    }

    public String getTentative() {
        return tentative;
    }

    public void setDescription(final String description) {
        this.description = description;
    }

    public String getDescription() {
        return description;
    }

    /**
     * @return
     */
    public double getFTE() {
        return FTE;
    }

    public void setFTE(final double fte) {
        FTE = fte;
    }

    /**
     * @param fTE
     *            the fTE to set
     */
    public void setFTE(final Double fTE) {
        FTE = fTE;
    }

    /**
     * @param availabilityStatus
     *            the availabilityStatus to set
     */
    public void setAvailabilityStatus(final AvailabilityStatus availabilityStatus) {
        this.availabilityStatus = availabilityStatus;
    }

    public AvailabilityStatus getAvailabilityStatus() {
        return availabilityStatus;
    }

    private AvailabilityStatus buildAvailabilityStatus(final AssignmentType assignmentType, final Boolean internal) {
        if (assignmentType == null)
            return null;
        if (assignmentType.equals(AssignmentType.AddOnAvailable))
            return AvailabilityStatus.AddOnAvailable;
        if (assignmentType.equals(AssignmentType.AddOnNotAvailable))
            return AvailabilityStatus.AddOnNotAvailable;
        if (assignmentType.equals(AssignmentType.Assigned))
            if (internal)
                return AvailabilityStatus.Interno;
            else
                return AvailabilityStatus.AsignadoCliente;
        return null;
    }

    private String buildName(final String firstName, final String middleName, final String lastName) {
        String name = "";
        if (lastName != null) {
            name += lastName;
        }
        if (!name.isEmpty() && !(StringUtils.isEmpty(firstName) && StringUtils.isEmpty(middleName))) {
            name += ", ";
        }
        if (firstName != null) {
            name += firstName;
        }
        if (middleName != null) {
            name += " " + middleName;
        }
        return name;
    }
}
